home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 1997 January / Software of the Month Club 1997 January.iso / pc / dos / child / velena / connect4.doc next >
Encoding:
Text File  |  1996-11-16  |  26.2 KB  |  636 lines

  1.  
  2.  
  3.                               -+- Velena -+-
  4.  
  5.          A Connect Four Expert System which Plays the Game Perfectly
  6.          ===========================================================
  7.  
  8.  
  9.  
  10.                                  Abstract
  11.                                  ========
  12.  
  13. Velena is a Shannon-C type strategy program written to play Connect Four.
  14. It's based on a knowledged approach that uses eight mathematical rules to
  15. take its decisions. Since the rules are proven to be correct, the conclusions
  16. made by the program are also correct. In this way it has been possible to
  17. show that Connect Four is a first player win and Velena is always able to
  18. win when she plays first.
  19.  
  20. ===============================================================================
  21.  
  22.  
  23. 1. Program overview
  24. ===================
  25.  
  26.     1.1. System Requirements
  27.     ========================
  28.  
  29.     To run Velena, you will need the following system:
  30.  
  31.     * An IBM compatible, 386 or better computer (486/DX2 suggested)
  32.     * Four megabytes of RAM.
  33.     * A SVGA VESA-compatible graphic adapter.
  34.     * MS-DOS 5.0 (or better) or compatible operating system.
  35.  
  36.  
  37.     1.2. Freeware agreement
  38.     =======================
  39.  
  40.     This program is to be considered freeware, which means that you can use
  41.     and distribute it to anyone you want, provided no fee is charged except
  42.     for media support.
  43.     You should also not disassemble modify or reverse engineer the program
  44.     for any reason in any circumstance.
  45.     The program is offered as is without warrants of any kind, even if the
  46.     best efforts have been made to produce a functional and efficient product.
  47.     I should not be liable for any damage caused by the use or misuse of
  48.     this program. Running the program is entirely at user's risk.
  49.  
  50.     Even if a fee is not required, I will be glad to receive a postcard from
  51.     your hometown with your comments and suggestion about my program.
  52.  
  53.  
  54.                                              The author current address is:
  55.  
  56.                                              Giuliano Bertoletti
  57.                                              Via Bocchialini n.6
  58.                                              Cap. 43100, Parma. Italy
  59.  
  60.                                              E-Mail:  gbe@aida.eng.unipr.it
  61.                                              Fidonet: 2:332/801.6
  62.  
  63.  
  64.  
  65.     1.3. Greetings
  66.     ==============
  67.  
  68.     This program is based on the knowledged approach of L.Victor Allis
  69.     which designed and implmented a sophisticated AI engine in a program
  70.     called Victor.
  71.     Velena is basically the same, except that even newer concepts and
  72.     techinques were introduced in order to reduce the problem complexity
  73.     (of solving the game) to a more tractable factor of magnitude.
  74.     Moreover Velena is available to anyone, while Victor (currently) is not.
  75.     I thank L.Victor Allis for his support while I developed Velena
  76.     and for the theory he made for solving Connect Four.
  77.     Without him this program wouldn't have come to light.
  78.  
  79.     I also thank Filippo Ghilardi who helped me to build the opening book
  80.     data base which took several days of work on his Pentium 133 computer,
  81.     and Davide Mazza who created the Velena intro logo.
  82.  
  83.  
  84.  
  85. 2. Introduction into Connect Four
  86. =================================
  87.  
  88. Now I'll describe the rules of the game as well as some nomenclature used
  89. throughout this manual and some basic connect four strategy.
  90.  
  91.  
  92.     2.1. Game Rules
  93.     ===============
  94.  
  95.     Connect Four is a two players game which takes place on a 7x6
  96.     rectangular board placed vertically between them. One player has
  97.     21 yellow men and the other 21 red men. Each player can drop
  98.     a man at the top of the board in one of the seven columns; the
  99.     man falls down and fills the lower unoccupied square. Of course
  100.     a player cannot drop a man in a certain column if it's already
  101.     full (i.e. it already contains six men).
  102.  
  103.     Even if there's no rule about which color should play first, in order
  104.     to avoid confusion we will assume that men are either white or black
  105.     (instead of yellow and red) and that white, as in chess, always plays
  106.     first.
  107.  
  108.     Note however that Velena displays men in yellow and red even if here are
  109.     refered as white and black respectively. Therefore when playing with
  110.     Velena, yellow always begins first.
  111.  
  112.     The object of the game is to connect four men vertically, horizzontally
  113.     or diagonally. If the board is filled and no one has alligned four men
  114.     then the game is drawn (that is after 42 moves if no one wins).
  115.  
  116.     Here's an example of a game won by white (O) and black (X) in fig.1 and
  117.     fig.2 respectively. A possible draw is represented in fig.3
  118.  
  119.  
  120.          .......                  .......                  OOOXOOO
  121.          .......                  .......                  XXXOXXX
  122.          .......                  .XO....                  OOOXOOO
  123.          ...X...                  .XXO...                  XXXOXXX
  124.          ...X...                  .OOX...                  OOOXOOO
  125.          XOOOO..                  .OXOX..                  XXXOXXX
  126.  
  127.           Fig.1                    Fig.2                    Fig.3
  128.       White (O) wins           Black (X) wins          The Game is drawn
  129.  
  130.  
  131.     2.2. Nomenclature
  132.     =================
  133.  
  134.     Since we need a way for representing a sequence of moves instead of a
  135.     position, I will use the same nomenclature as the one used for chess.
  136.     This means that columns are named from A to G, starting from left, and
  137.     rows are numbered from 1 to 6 starting from the bottom.
  138.     In this way we could represent each square of the board with a pair
  139.     letter-number. For example the square in the middle of the lowest row
  140.     is d1. The square above is d2 and the square on the left of d1 is c1.
  141.     (see fig. 3)
  142.  
  143.                               6 . . . . . . .
  144.                               5 . . . . . . .
  145.                               4 . . . . . . .
  146.                               3 . . . . . . .
  147.                               2 . . . . . . .
  148.                               1 . . . . . . .
  149.                                 A B C D E F G
  150.  
  151.                                    Fig. 3
  152.  
  153.  
  154.     In this way we could write down a game as a sequence of moves.
  155.     For example the endgame of fig.1 could have arisen from the following
  156.     sequence of moves:
  157.  
  158.         1) d1,d2
  159.         2) c1,d3
  160.         3) b1,a1
  161.         4) e1++
  162.  
  163.     where ++ indicates the player who did that move won the game (as in
  164.     chess).
  165.  
  166.  
  167.     2.3. Game Strategy
  168.     ==================
  169.  
  170.     There are two kinds of strategies in connect four. The first consist in
  171.     looking ahead few moves to avoid the opponent to win and in the same time
  172.     trying to connect four men. The other is looking for a win in the long run.
  173.  
  174.     Virtually all the alghoritms I have seen tend to implement the first
  175.     strategy with some variants of alpha beta search and the most
  176.     sophisticated ones with tree branch pruning. These strategies assure more
  177.     or less the unvulnerability in the short run, but they are doomed to fail
  178.     in the long run because they cannot see beyond the horizzon of few plies.
  179.  
  180.     Most of the games ends between 35th and 42nd move when you or the opponent
  181.     is forced to make a particular move since there's only one column
  182.     available. In this circumstance most of the people believe that the winner
  183.     is lucky (if there's one). That's not it. An expert player is able to
  184.     make this happen much time before. Actually this is what Velena does.
  185.  
  186.     The first step consist in noting that after white has moved an odd number
  187.     of free squares remains on the board. Similary after black has moved an
  188.     even number of free square remains. When there's only one column available
  189.     it's clear that the last square will be occupied by black (if no one wins
  190.     first).
  191.  
  192.     Now let introduce some definitions before continuing:
  193.  
  194.     -----------------------------------------------------------------------
  195.  
  196.     ODD SQUARE: it's a square belonging to an odd row. For example d1,c1,c3,
  197.     f5 are all odd squares.
  198.  
  199.     EVEN SQUARE: same as above except that the row is even. For example a2,
  200.     b4,c6,e2 are all even squares.
  201.  
  202.     GROUP: it's a set of four squares connected horizzontally, vertically
  203.     or diagonally. The first player who fills a group with his men, wins.
  204.  
  205.     THREAT: it's a group filled with three men of the same color which has
  206.     the fourth square empty, and also the square below the empty square is
  207.     empty.
  208.  
  209.     ODD THREAT: it's a therat in a group whose empty square is odd.
  210.  
  211.     EVEN THREAT: same as above but the empty square is even.
  212.  
  213.     DOUBLE THREAT: they are two groups which share an empty odd square;
  214.     each group is filled with only two men (of the same color) and the
  215.     other two squares (one for each group) are empty and are one above
  216.     the other. The square below the shared square must be empty too.
  217.  
  218.     -----------------------------------------------------------------------
  219.  
  220.     It's then clear that if white has an odd threat and black cannot connect
  221.     four men anywhere, the game will be eventually won by white.
  222.  
  223.     Please note that this is a sufficient condition and if black can connect
  224.     four men somewhere, further knowledge is required.
  225.  
  226.     Similary if black has an even threat and white cannot connect four men the
  227.     game will be eventually won by black.
  228.  
  229.     It's clear that in both cases we assume that players make the best move
  230.     available.
  231.  
  232.     Things get more complex when both players have at least one group in
  233.     which they can connect four men. In this case we need further
  234.     investigation.
  235.  
  236.     If white has an odd threat and black has an even threat and the columns
  237.     in which the threats are (i.e. the empty square) are different then
  238.     white can still win. Of course no player must be able to connect four men
  239.     except in the group in which he has the odd/even threat stated above.
  240.  
  241.     But if columns are the same then the lower threat (i.e. the lower
  242.     empty square) wins.
  243.  
  244.     If both players have an odd threat the game will be drawn. Unless one
  245.     of them can connect four men elsewhere.
  246.  
  247.     Then the strategy for white is to try to obtain an odd threat and for
  248.     black an even threat. This is not always sufficient as the restrictions
  249.     above shows but it's a good starting point to play connect four,
  250.     especially when both players are humans.
  251.  
  252.  
  253.  
  254.     2.4. The Way Velena Plays
  255.     =========================
  256.  
  257.     When set to her strongest level, Velena uses two different strategies
  258.     according she's playing white or black. In both cases she uses a database
  259.     for the opening lines. The database is made in such a way that Velena is
  260.     always able to reach a position in which she has an odd threat when she
  261.     plays white (and from there she's able to continue and win by herself).
  262.     She tries to follow the longest winning line for white when she plays with
  263.     black. The built in evaluation function which examines the positions is
  264.     then sufficient to play the middle and end game. However, further search
  265.     is done just to check for trivial wins few moves ahed.
  266.  
  267.  
  268.     2.5. Drawbacks of the alghoritm
  269.     ===============================
  270.  
  271.     Connect Four is not complex like chess. Therefore moves tends to repeat
  272.     many times. For example the winning line for white is very narrow, so
  273.     narrow that the first seven moves for white are forced.
  274.     This is the reason why Velena is forced to answer almost always in the
  275.     same way, given the same position on the board. There are not many
  276.     variants.
  277.  
  278.     It's not strange that a player who keeps white men and learns how to
  279.     win a game, is then always able to win each time he plays. This because
  280.     he can play the same game each time.
  281.  
  282.     Another drawback is that Velena pays very poor attention to distinguish
  283.     between a win for black and a draw. They are almost the same thing for her.
  284.  
  285.     Also note that Velena does the best move (when playing with white) only
  286.     when she starts from the empty board. If someone sets up a position and
  287.     then tells the computer to continue the game from that point, it's not
  288.     warranted that the computer plays at best.
  289.  
  290.     Finally the algorithm tend to resign when a game is compromised and
  291.     doesn't fight until the end.
  292.  
  293.  
  294. 3. Running Velena
  295. =================
  296.  
  297. After you have installed Velena on your hard disk, you can start the program
  298. simply typing "connect4" at the dos prompt. As already indicated in section
  299. 1.1, the program needs among the other things a SVGA VESA compatible card.
  300. Many cards can be made VESA compatible simply with a device driver that is
  301. in most of the cases provided with the graphic adapter itself. If you have
  302. any problem, look in your card manual. Besides there are also many freeware
  303. device drivers which can make your graphic adapter become VESA compatible.
  304.  
  305. After a few seconds the computer will display the logo and then the board.
  306. Velena is ready to play.
  307.  
  308. The program is entirely mouse driven and the commands are available as
  309. click-able gadgets in the top of the screen.
  310.  
  311.     3.1. The Control Panel
  312.     ======================
  313.  
  314.     As you surely already noticed, Connect Four is very simple to understand
  315.     and doesn't require too many operations to control the game options.
  316.     The buttons in the top of the screen can drive the program with basic
  317.     commands. Let's see their meaning:
  318.  
  319.  
  320.       - New Game
  321.  
  322.         When you want to begin a new game, click this button and then
  323.         confirm the option by answering YES. If you make a mistake or
  324.         you change your mind you can answer NO and the command will
  325.         be ignored.
  326.  
  327.  
  328.       - Options
  329.  
  330.         When you want to set up a match between a player and the computer
  331.         or two players, or an autoplay, click this button.
  332.         You can also choose the computer level this way. By default red
  333.         side is kept by Velena and yellow side (which begins first) is kept
  334.         by the player. The default computer level is "strong".
  335.  
  336.         If you are unable to win (which is most likely), you can lower the
  337.         level to "normal" or "weak".
  338.  
  339.  
  340.       - Hint
  341.  
  342.         If you need a hint click this button and the computer will move
  343.         for you.
  344.  
  345.  
  346.       - Take Back
  347.  
  348.         This is probably the most used option. If you make a mistake and
  349.         you want to undo the move made, just click this button.
  350.         Note that when a two players game is set, only the last move is
  351.         taken back. But when you play against the computer, moves are
  352.         taken back two at time because you can take back only after the
  353.         computer has made it's move.
  354.         You can also take back more than one move by clicking repeatedly
  355.         this button.
  356.  
  357.  
  358.       - About
  359.  
  360.         Shows the program current version with other informations about
  361.         the author and greetings.
  362.  
  363.  
  364.       - Quit
  365.  
  366.         When you finished to play with Velena you can click this button
  367.         and confirm you really want to quit.
  368.  
  369.  
  370.     3.2. Other commands
  371.     ===================
  372.  
  373.     Moves are made by pointing the square in which the player in turn
  374.     wants to play. You can also move with the keyboard by pressing keys
  375.     1..7, where 1 is the leftmost column and 7 the rightmost.
  376.  
  377.     F1 key can be used to flip colors on the screen (although men are always
  378.     referenced as black and white).
  379.  
  380.     A file named CONNECT4.LOG is always updated on disk each time a game is
  381.     over and keeps track of all the games played.
  382.  
  383.  
  384.     3.3. Advanced options
  385.     =====================
  386.  
  387.     With the right mouse button you can access a pull down menu with some
  388.     advanced features. Here a brief description of what they mean:
  389.  
  390.  
  391.        - Flash last move:
  392.  
  393.        It can be use to replay the last move made. The last man flashes for
  394.        few seconds to let you understand for example where the computer moved.
  395.  
  396.  
  397.        - Show moves list:
  398.  
  399.        Shows the moves so far in the chess notation described above.
  400.        It also shows the moves in a column-wise manner which denotes only
  401.        the column in which a player moved.
  402.  
  403.  
  404.        - Position analysis
  405.  
  406.        Analyzes the current position on the board (for the side which is NOT
  407.        in turn to move) and tells which is the theorical result that can be
  408.        achieved if that player plays at best. Please note that only sufficient
  409.        conditions are used for the analysis. This means that if the computer
  410.        tells a game can be won, this is true, but if the computer says nothing
  411.        this doesn't mean that a game can't be won. In other words it proceeds
  412.        by sufficient conditions, not necessary.
  413.  
  414.  
  415.        - Oracle analysis
  416.  
  417.        This is probably the most difficult feature to explain. It gives a
  418.        detailed report of the oracle status (which is one of the three engines
  419.        which drive Velena, and besides the most important one).
  420.        Since rules definitions and applications are difficult to understand,
  421.        I suggest you to refer to L.V.Allis thesis "A knowlede based approach
  422.        of Connect Four" to know more. Once you red and understood that thesis
  423.        you'll be able to understand the report Velena gives with this option.
  424.        Refer to paragraph 4.8 for further informations.
  425.        Please note that also the oracle proceeds by sufficient conditions;
  426.        there are some positions where the game result is clear and the oracle
  427.        seems not to foresee it.
  428.  
  429.  
  430.        - Reset counters
  431.  
  432.        Resets the two counters which keep players score (shown at the top
  433.        of the board).
  434.  
  435.  
  436.        - Change colors
  437.  
  438.        It simply changes the palette, swapping the three main colors
  439.        components. This leads to a total of six different combinations.
  440.  
  441.  
  442.        - Save moves list
  443.  
  444.        Writes to disk an ascii file with the moves made. The file name is
  445.        choosen automatically.
  446.  
  447.  
  448.        - Save screen to disk
  449.  
  450.        Writes an image in GIF format with the current position on the board.
  451.        The GIF can be in two colors or in 256 colors according to your needs.
  452.        The 256 color GIF is the current Velena screen, while the 2 colors
  453.        screen is simply a diagram which is pretty useful if you want to print
  454.        it.
  455.  
  456.  
  457.  
  458. 4. Why Velena is so special
  459. ===========================
  460.  
  461. As already said before, Velena is an expert system able to play the game
  462. perfectly. This means that if the program is set to it's maximum level of
  463. strength she always wins when she plays first and she is almost impossible
  464. to beat when she plays second (until you make a few games and you will
  465. learn how to beat her by the program herself).
  466.  
  467. Of course if you are a beginner you can set a lower difficulty level for the
  468. computer. This is useful if you are interested in learning how to play.
  469.  
  470.  
  471.     4.1. Game complexity
  472.     ====================
  473.  
  474.     Although at first sight Connect Four doesn't seem a very complex game
  475.     in terms of combinations of moves and the number of reachable different
  476.     positions on the board, it should be noticed that it's not so a trivial
  477.     game as it looks. Let's see some mathematics behind it.
  478.  
  479.     First we can make a raw estimation of game complexity noting that since
  480.     each square can be empty or occupied by either white or black, each
  481.     square can be in only three different states, leading a total game
  482.     complexity of 3^42 which is in the order of 10^20. Of course this is an
  483.     upper bound which takes in count also the illegal positions.
  484.  
  485.     It is possible to make some finer estimations that reduces the number of
  486.     legal positions, anyway even the finest estimation gives us an order
  487.     of magnitude of 7.1 x 10^13, which is still to high for a complete
  488.     enumeration. To build up a brute force attack several terabytes of disk
  489.     space would be required, and current technology is far away from this
  490.     possibility. Besides the space requirements, also the time requirements
  491.     would be out of what are called "reasonable resources". Finally it would
  492.     be almost impossible to distribute the program.
  493.  
  494.  
  495.     4.2. So Velena is not a brute force attack against Connect Four?
  496.     ================================================================
  497.  
  498.     Definitly not. Velena is an intelligent program which is able to
  499.     predict the final result of a game using complex mathematics, and
  500.     efficient search strategies. The program is compact and strong;
  501.     only a tiny opening book of about 60.000 positions is used for the
  502.     opening lines. All the rest is calculated on fly.
  503.  
  504.  
  505.     4.3. So why should I use Velena if it's unbeatible?
  506.     ===================================================
  507.  
  508.     Well, there are several reasons why this program can be useful.
  509.     First it represents the final word on Connect 4 (or so I hope):
  510.     no program or living man can outperform Velena.
  511.  
  512.     Besides, Velena is very useful to train yourself against a human
  513.     player. If you want to become a strong player then you should train
  514.     yourself with a strong player. And Velena is the strongest.
  515.  
  516.  
  517.     4.4. But is Velena really so perfect?
  518.     =====================================
  519.  
  520.     Well, it's perfect in the sense that she's always able to win if she
  521.     plays first. This because it can be mathematically shown that Connect
  522.     Four can be won by the player which plays first (if it plays well) no
  523.     matter how good the opponent is.
  524.  
  525.     Of course if Velena plays second, there's still a chance for the human
  526.     player to defeat her. Once you learned how, you can easiliy win.
  527.     More over the purpose of the computer in this case (when playing second)
  528.     is not to win, but to avoid losing, if possible. In other words Velena
  529.     considers a draw a good result if it plays with red men.
  530.  
  531.  
  532.     4.5. So Velena destroyed Connect Four?
  533.     ======================================
  534.  
  535.     Yes, but humans are humans and machines are machines. Connect Four is
  536.     still interesting if played between two men. Of course there are no
  537.     chances between a man and a computer, like there are no chances between
  538.     a man and a car. The latter will run always faster.
  539.  
  540.  
  541.     4.6. Why does Velena play almost always the same game when she's
  542.     in autoplay?
  543.     =================================================================
  544.  
  545.     Connect four is not like chess. Even if the game is not as trivial
  546.     as tic tac toe, the complexity is much smaller than chess. This of
  547.     course leads to a limited number of variants and often the moves are
  548.     forced. For example there's only one winning way for white and the
  549.     first seven moves are forced for him. If he choses another move, black
  550.     can draw. Similary black has only one strong defensive line (which of
  551.     course falls in the long run) which can protect him from a premature
  552.     loss. It's clear that once you learn how to infiltrate in this line,
  553.     you are likely to win each time against black.
  554.  
  555.  
  556.     4.7. Isn't Velena too slow in replying?
  557.     =======================================
  558.  
  559.     It depends on which machine she runs. Theorically it can run on a 386
  560.     based processor, but she surely will take a lot of time. At least a
  561.     486DX2 is required to play at decent speed.
  562.     Anyway the algorithm behind is very complex, and no time is wasted in
  563.     useless delays. Velena needs all the resources she uses. Believe me!
  564.  
  565.  
  566.     4.8. Where can I find the complete description of the algorithms used here?
  567.     ===========================================================================
  568.  
  569.     You can try:
  570.  
  571.     ftp://ftp.cs.vu.nl/~victor/connect4.zip
  572.     ftp://ftp.cs.vu.nl/~victor/thesis.zip
  573.  
  574.     this is the documentation made available by L.Victor Allis and surely
  575.     it's very interesting. Of course I am not sure that it will be there
  576.     forever. Please contact victor@cs.vu.nl for more informations.
  577.  
  578.     Keep also in mind that this program is based on his theory but it does
  579.     not follow it completely. For example the mathematical rules have been
  580.     reduced from nine to eight.
  581.  
  582.     You can also refer to Velena Home Page at:
  583.     http://www.ce.unipr.it/~gbe/velena.html
  584.  
  585.  
  586.     4.9. Where can I get the last version of Velena
  587.     ===============================================
  588.  
  589.     Check out Velena's Home Page at:
  590.  
  591.     http://www.ce.unipr.it/~gbe/velena.html
  592.     or use a net serach engine.
  593.  
  594.  
  595.     4.10. Is the source code of Velena available to the public?
  596.     ===========================================================
  597.  
  598.     No, at the moment it's not. Maybe one day it will.
  599.  
  600.  
  601.     4.11. Who's F.Alinovi and why did he say: "...who wants to make four?"
  602.     ======================================================================
  603.  
  604.     He's a friend. After I bored him telling the inner workings of
  605.     Velena and the clever idea behind the algorithm he answered that way.
  606.     He also added that I was losing my time. Poor little fellow! :))))
  607.  
  608.  
  609.     4.12. What's "a Shannon-C type program" mean?
  610.  
  611.     In his famous paper in 1950 C.Shannon describes three methods by which
  612.     a machine could play a strategy game (such chess, checkers, connect four
  613.     and so on...). The C-type method consist in emulating human mind to take
  614.     decisions. In other words the eight mathematical rules Velena uses are
  615.     not based on a search algorithm but on the properties of the game.
  616.     The A-type method instead is based on pure brute force search.
  617.     Just to be fair Velena is not only a Shannon-C type program since it
  618.     also uses some search algoritms to play, anyway the great difference in
  619.     playing capabilities relies just on this knowledged approach, which made
  620.     possible to solve the game.
  621.  
  622.  
  623. ===============================================================================
  624.  
  625.     Further details will be given if requested. Please write in english or
  626.     in italian.
  627.  
  628. ===============================================================================
  629.  
  630.                                                      The author:
  631.                                                      Giuliano Bertoletti
  632.                                                      Via Bocchialini n.6
  633.                                                      Cap. 43100 Parma
  634.                                                      gbe@aida.eng.unipr.it
  635.  
  636.